// File Name: usart_interface.c
// Author: kukukukuku~
// Date: 2024-12-21

#include "string.h"
#include "./USART/inc/usart_interface.h"

/**
 * @brief   USART对象初始化
 * @param   usartObj:   USART对象
 * @param   txBuffer:   USART发送缓冲区
 * @param   rxBuffer:   USART接收缓冲区
 * @retval  None
 * @note    None
*/
void usart_object_init(USART_OBJECT_T * const usartObj, const uint8_t * const txBuffer, const uint8_t * const rxBuffer)
{
    if(usartObj == NULL || txBuffer == NULL || rxBuffer == NULL || txBuffer == rxBuffer)
        return;
    
    usartObj->pTxBuffer = (uint8_t *)txBuffer;
    usartObj->pRxBuffer = (uint8_t *)rxBuffer;
    usartObj->rxFinishFlag = USART_RX_STATE_UNCOMPLETE;
    usartObj->rxIndex = 0;
}


/**
 * @brief   USART空闲中断回调函数
 * @param   usartObj:   USART对象
 * @retval  None
 * @note    None
*/
void usart_int_idle_callback(USART_OBJECT_T * const usartObj)
{
    if(usartObj == NULL)
        return;
    
    usartObj->rxFinishFlag = USART_RX_STATE_COMPLETE;
}


/**
 * @brief   USART接收中断回调函数
 * @param   usartObj:   USART对象
 * @retval  None
 * @note    None
*/
void usart_int_rx_callback(USART_OBJECT_T * const usartObj)
{
    if(usartObj == NULL || usartObj->pRxBuffer == NULL)
        return;
    
    if(usartObj->rxFinishFlag == USART_RX_STATE_UNCOMPLETE && usartObj->rxIndex < USART_RX_BUFFER_LEN)
        usartObj->pRxBuffer[usartObj->rxIndex++] = usart_usart_receive_data();
}


/**
 * @brief   USART清除接收完成标志
 * @param   usartObj:   USART对象
 * @retval  None
 * @note    None
*/
void usart_rx_finish_flag_clear(USART_OBJECT_T * const usartObj)
{
    if(usartObj == NULL)
        return;
    
    usartObj->rxFinishFlag = USART_RX_STATE_UNCOMPLETE;
}


/**
 * @brief   USART获取接收完成标志
 * @param   usartObj:   USART对象
 * @retval  接收完成标志
 *          @arg USART_RX_STATE_UNCOMPLETE: 接收未完成
 *          @arg USART_RX_STATE_COMPLETE:   接收已完成
 * @note    None
*/
USART_RX_STATE_T usart_rx_finish_flag_get(const USART_OBJECT_T * const usartObj)
{
    if(usartObj == NULL)
        return USART_RX_STATE_UNCOMPLETE;
    
    return usartObj->rxFinishFlag;
}


/**
 * @brief   USART清除接收数据长度
 * @param   usartObj:   USART对象
 * @retval  None
 * @note    None
*/
void usart_rx_data_len_clear(USART_OBJECT_T * const usartObj)
{
    if(usartObj == NULL)
        return;
    
    usartObj->rxIndex = 0;
}


/**
 * @brief   USART获取接收数据长度
 * @param   usartObj:   USART对象
 * @retval  接收数据长度
 * @note    None
*/
uint16_t usart_rx_data_len_get(const USART_OBJECT_T * const usartObj)
{
    return usartObj == NULL ? 0 : usartObj->rxIndex;
}


/**
 * @brief   USART接收数据处理
 * @param   usartObj:   USART对象
 * @retval  None
 * @note    None
*/
void usart_received_data_handle(USART_OBJECT_T * const usartObj)
{
    if(usartObj == NULL || usartObj->pRxBuffer == NULL)
        return;
    
    //数据处理
}


/**
 * @brief   USART获取接收缓冲区
 * @param   usartObj:   USART对象
 * @retval  接收缓冲区
 * @note    None
*/
uint8_t * usart_rx_buffer_get(const USART_OBJECT_T * const usartObj)
{
    return usartObj == NULL ? NULL : usartObj->pRxBuffer;
}


/**
 * @brief   USART清空接收缓冲区
 * @param   usartObj:   USART对象
 * @retval  None
 * @note    None
*/
void usart_rx_buffer_clear(USART_OBJECT_T * const usartObj)
{
    if(usartObj == NULL || usartObj->pRxBuffer == NULL)
        return;
    
    memset(usartObj->pRxBuffer, 0, USART_RX_BUFFER_LEN);
}


/**
 * @brief   USART发送数据
 * @param   usartObj:   USART对象
 * @param   data:       要发送的数据
 * @param   dataLen:    要发送的数据长度
 * @retval  None
 * @note    None
*/
void usart_send_data(USART_OBJECT_T * const usartObj, const uint8_t * const data, const uint16_t dataLen)
{
    if(usartObj == NULL || usartObj->pTxBuffer == NULL || data == NULL || dataLen == 0 || dataLen > USART_TX_BUFFER_LEN)
        return;
    
    memset(usartObj->pTxBuffer, 0, USART_TX_BUFFER_LEN);
    memcpy(usartObj->pTxBuffer, data, dataLen);
    
    usart_usart_send_data(usartObj->pTxBuffer, dataLen);
}
